var express = require('express');
var router = express.Router();
var path = require('path');
var connectDB = require('../connectDB');
/* GET home page. */
router.get('/', function (req, res, next) {
    let pathUrl = path.join(__dirname + '.\/../../views/lxcdingdanshouhuodizhi.html');
    res.sendFile(pathUrl);

});
router.post('/xiugai', async function (req, res, next) {

    let lxc = await funxiugai(req.body['past[]']);
    res.send(lxc);


});
router.post('/zhengjia', async function (req, res, next) {
    req.body['past[]'].unshift(null)

    console.log(req.body['past[]']);
    let lxc = await funzhengjia(req.body['past[]']);
    res.send(lxc);

});

// 'SELECT * FROM address where user_id = ?,userName = ?,address = ?,tel = ? ',[2,'超爷爷','村里的孩子'

// funxiugai(['超爷爷','村里的孩子','000000',2]);
function funxiugai(data) {
    return new Promise((a, b) => {
        console.log(data);
        
        connectDB.query('UPDATE address SET userName = ?,address = ?,tel = ? WHERE id = ?', data, (error, results, fields) => {
            if (!error) {
                console.log('修改成功');

                a(JSON.stringify(results));
            }
        })
    })

};

// funzhengjia([null,1,'超','村里的孩子','000000',0]);
function funzhengjia(data) {
    return new Promise((a, b) => {
        connectDB.query('INSERT INTO address values  (?,?,?,?,?,?)', data, (error, results, fields) => {
            if (!error) {
                console.log('插入成功');
                a(JSON.stringify(results));
            }
        })
    })
};















module.exports = router;